package com.houzehui.mncity.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.houzehui.mncity.config.MsgResult;
import com.houzehui.mncity.entity.City;
import com.houzehui.mncity.service.ICityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author houzehui
 * @since 2022-04-08
 */
@RestController
@RequestMapping("/city")
public class CityController {

    @Autowired
    ICityService iCityService;

    @GetMapping("getcity")
    public City getcity(City city){
        System.err.println(city);

        City city2 = new City();
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        QueryWrapper<City> wrapper2 = new QueryWrapper<>();
        QueryWrapper<City> wrapper3 = new QueryWrapper<>();
        //查询省
        wrapper.eq("id",city.getProvinceid());
        City city1 = iCityService.getBaseMapper().selectOne(wrapper);
        city2.setProvincename(city1.getCityname());
        //查询市
        wrapper2.eq("id",city.getCityid());
        City city3 = iCityService.getBaseMapper().selectOne(wrapper2);
        city2.setCityname1(city3.getCityname());

        //查询省
        wrapper3.eq("id",city.getCountyid());
        City city5 = iCityService.getBaseMapper().selectOne(wrapper3);
        city2.setCountyname(city5.getCityname());


        System.err.println(city2);
        return city2;

    }


    @GetMapping("getcs")
    public List<City> getcs(){
        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.eq("pid",1);
        List<City> list = iCityService.list(wrapper);

        list.forEach(cityids->{
            List<City> id = iCityService.list(new QueryWrapper<City>().eq("pid", cityids.getId()));
            cityids.setChildren(id);
            id.forEach(countyids->{
                List<City> pid = iCityService.list(new QueryWrapper<City>().eq("pid", countyids.getId()));
                countyids.setChildren(pid);
            });
        });

        return list;


    }

}
